package com.fly.docker.controller;

import java.io.File;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
public class IndexController
{
    @Autowired
    HttpSession httpSession;
    
    File root = new File("upload");
    
    @Value("#{'${test.list}'.split(',')}")
    private List<String> list;
    
    @Value("${test.list2}")
    private List<String> list2;
    
    @Value("${test.cloud.list3}")
    private String[] array;
    
    @PostConstruct
    private void init()
    {
        root.mkdirs();
    }
    
    @GetMapping({"/", "/index"})
    public String index(Model model)
    {
        log.info("list={}", list);
        log.info("list2={}", list2);
        log.info("array={}", array.length);
        if (root.exists())
        {
            List<File> files = FileUtils.listFiles(root, null, true).stream().filter(f -> f.isFile()).sorted(Comparator.comparing(File::getAbsolutePath)).collect(Collectors.toList());
            model.addAttribute("files", files);
            model.addAttribute("size", FileUtils.byteCountToDisplaySize(FileUtils.sizeOfDirectory(root)));
        }
        return "index";
    }
}
